<template>
  <div class="chat-window">
    <div 
      v-for="(msg, index) in messages" 
      :key="index" 
      class="message"
      :class="{'user-msg': msg.role === 'user', 'ai-msg': msg.role === 'assistant'}"
    >
      <div class="msg-role">{{ msg.role === 'user' ? 'You' : 'AI' }}</div>
      <div class="message-content">
        <div v-html="formatContent(msg.content)"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    messages: Array
  },
  methods: {
    formatContent(text) {
      // 流式输出处理
      return text
        .replace(/\n/g, '<br>')
        .replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>')
        .replace(/`([^`]+)`/g, '<code>$1</code>')
        .replace(/(▎|)/g, '<span class="cursor-animate">$1</span>');
    },
    
    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$el.querySelector('.chat-window');
        container.scrollTop = container.scrollHeight;
      });
    }
  }
}
</script>

<style scoped>
.chat-window {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.message {
  margin: 15px 0;
  padding: 12px;
  border-radius: 8px;
  max-width: 80%;
}

.user-msg {
  background: #e3f2fd;
  margin-left: auto;
}

.ai-msg {
  background: #f5f5f5;
  margin-right: auto;
}

.msg-role {
  font-weight: bold;
  margin-bottom: 5px;
  color: #666;
}

pre {
  background: #f8f8f8;
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
}

code {
  font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
  font-size: 0.9em;
}

pre {
  position: relative;
  margin: 1em 0;
  border-radius: 8px;
  overflow: hidden;
}

pre code {
  display: block;
  padding: 16px;
  line-height: 1.5;
}

pre::after {
  content: "";
  position: absolute;
  right: 12px;
  top: 12px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #e1e4e8;
}

pre[class*="language-"]::after {
  background: #ff5f56;
}
</style>
